package com.example.zsfinacialmanage.activity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.db.MyDBHelper;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import java.util.ArrayList;
import java.util.List;
public class DataAnalyseActivity extends AppCompatActivity {
    //定义对象
    LineChart income_chart,outpay_chart;
    MyDBHelper mhelper;
    SQLiteDatabase db;
    String[] indata = {"学习-奖金","补助-奖金","比赛-奖金","业余-兼职","基本-工资","福利-分红","加班-津贴","其他"};
    //收入类型数据统计的初始值
    int xxjjmoney = 0;
    int bzjjmoney = 0;
    int bsjlmoney = 0;
    int yyjzmoney = 0;
    int jbgzmoney = 0;
    int flfhmoney = 0;
    int jbjtmoney = 0;
    int qtmmoney = 0;
    String [] outdata ={"电影-娱乐","美食-畅饮","欢乐-购物","手机-充值","交通-出行","教育-培训","社交-礼仪","生活-日用","其他"};
    int dyylmmoney = 0;
    int mscymoney = 0;
    int hlgwmmoney = 0;
    int sjczmmoney = 0;
    int jtcxmmoney = 0;
    int jypxmmoney = 0;
    int sjlymmoney = 0;
    int shrymmoney = 0;
    int othermoney = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_analyse);
        //绑定控件
        initView();
        //收入汇总分析
        inComeData();
        //支出汇总分析
        outComeData();
        //income_chart.invalidate(); // 刷新图表
    }
    //2.绑定控件
    private void initView() {
        income_chart=findViewById(R.id.income_chart_data);
        outpay_chart=findViewById(R.id.outpay_chart_data);
        mhelper = new MyDBHelper(DataAnalyseActivity.this);
        db = mhelper.getWritableDatabase();
    }
    //3.收入汇总分析
    private void inComeData() {
        //1.获取数据
        Cursor cursor = db.rawQuery("select * from in_come", null);
        //2.遍历数据
        while (cursor.moveToNext()) {
            @SuppressLint("Range")Double mymoney = cursor.getDouble(cursor.getColumnIndex("inmoney"));
            @SuppressLint("Range")String mytype = cursor.getString(cursor.getColumnIndex("intype"));
            if (mytype.equals("学习-奖金")){
                xxjjmoney += mymoney;
            }else if(mytype.equals("补助-奖金")){
                bsjlmoney += mymoney;
            }else if(mytype.equals("比赛-奖金")){
                bzjjmoney += mymoney;
            }else if(mytype.equals("业余-兼职")){
                yyjzmoney += mymoney;
            }else if(mytype.equals("基本-工资")){
                jbgzmoney += mymoney;
            }else if(mytype.equals("福利-分红")){
                flfhmoney += mymoney;
            }else if(mytype.equals("加班-津贴")){
                jbjtmoney += mymoney;
            }else if(mytype.equals("其他")){
                qtmmoney += mymoney;
            }
        }
        //cursor.close(); // 关闭游标
        //第二部分：LineChart图表初始化设置-XY轴的设置
        XAxis xAxis = income_chart.getXAxis();
        YAxis yAxisleft = income_chart.getAxisLeft();
        YAxis yAxisright = income_chart.getAxisRight();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        yAxisleft.setAxisMinimum(0f);
        yAxisright.setAxisMinimum(0f);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float v, AxisBase axisBase) {
                return indata[(int) v];
            }
        });
        //第三部分：LinDataSet曲线初始化设置
        List<Entry> inentries = new ArrayList<>();
        inentries.add(new Entry(0,xxjjmoney));
        inentries.add(new Entry(1,bzjjmoney));
        inentries.add(new Entry(2,bsjlmoney));
        inentries.add(new Entry(3,yyjzmoney));
        inentries.add(new Entry(4,jbgzmoney));
        inentries.add(new Entry(5,flfhmoney));
        inentries.add(new Entry(6,jbjtmoney));
        inentries.add(new Entry(7,qtmmoney));
        LineDataSet lineDataSet = new LineDataSet(inentries,"金额");
        lineDataSet.setValueTextSize(25);
        lineDataSet.setValueTextColor(Color.WHITE);
        lineDataSet.setDrawFilled(true);
        //第四部分：曲线展示
        LineData data = new LineData(lineDataSet);
        income_chart.setData(data);
    }
    //4.支出汇总分析
    private void outComeData(){
        //1.获取数据
        Cursor cursor = db.rawQuery("select * from pay_out", null);
        //2.遍历数据
        while (cursor.moveToNext()) {
            @SuppressLint("Range")Double mymoney = cursor.getDouble(cursor.getColumnIndex("outmoney"));
            @SuppressLint("Range")String mytype = cursor.getString(cursor.getColumnIndex("outtype"));
            if (mytype.equals("电影-娱乐")){
                dyylmmoney += mymoney;
            }else if(mytype.equals("美食-畅饮")){
                mscymoney += mymoney;
            }else if(mytype.equals("欢乐-购物")){
                hlgwmmoney += mymoney;
            }else if(mytype.equals("手机-充值")){
                sjczmmoney += mymoney;
            }else if(mytype.equals("交通-出行")){
                jtcxmmoney += mymoney;
            }else if(mytype.equals("教育-培训")){
                jypxmmoney += mymoney;
            }else if(mytype.equals("社交-礼仪")){
                sjlymmoney += mymoney;
            }else if(mytype.equals("生活-日用")) {
                shrymmoney += mymoney;
            }else if(mytype.equals("其他")){
                othermoney += mymoney;
            }
            }
        //第二部分：LineChart图表初始化设置-XY轴的设置
        XAxis xAxis = outpay_chart.getXAxis();
        YAxis yAxisleft = outpay_chart.getAxisLeft();
        YAxis yAxisright = outpay_chart.getAxisRight();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        yAxisleft.setAxisMinimum(0f);
        yAxisright.setAxisMinimum(0f);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float v, AxisBase axisBase) {
                return outdata[(int)v];
            }
        });
        //第三部分：LinDataSet曲线初始化设置
        List<Entry> outentries = new ArrayList<>();
        outentries.add(new Entry(0,dyylmmoney));
        outentries.add(new Entry(1,mscymoney));
        outentries.add(new Entry(2,hlgwmmoney));
        outentries.add(new Entry(3,sjczmmoney));
        outentries.add(new Entry(4,jtcxmmoney));
        outentries.add(new Entry(5,jypxmmoney));
        outentries.add(new Entry(6,sjlymmoney));
        outentries.add(new Entry(7,shrymmoney));
        outentries.add(new Entry(8,othermoney));
        LineDataSet lineDataSet = new LineDataSet(outentries,"金额");
        lineDataSet.setValueTextSize(20);
        lineDataSet.setValueTextColor(Color.WHITE);
        lineDataSet.setDrawFilled(true);
        //第四部分：曲线展示
        LineData data = new LineData(lineDataSet);
        outpay_chart.setData(data);
        }
    }
//        lineDataSet.setValueTextSize(20);
//        lineDataSet.setValueTextColor(Color.WHITE);
//        lineDataSet.setDrawFilled(false);
//        lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
//        lineDataSet.setColor(Color.RED);